<template>
  <a-modal
    title="修改"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    okText="确定"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="任务名称">
          <a-input v-decorator="['task_name', {rules: [{required: true, message: '请输入任务名称'}]}]" />
        </a-form-item>

        <a-form-item label="名称分类">
          <a-select
            v-decorator="['taskcate_id', {rules: [{required: true, message: '请选择任务的分类'}]}]" 
            show-search
            placeholder="请选择"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option :value="item.id" v-for="(item) in taskList" :key="item.id">
              {{item.title}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="发布时间">
          <a-date-picker v-decorator="['release_time']"
            show-time
            valueFormat="YYYY-MM-DD HH:mm:ss"
            type="date"
            placeholder="发布时间"
            style="width: 100%;"
          />
        </a-form-item>
        <a-form-item label="截至时间">
          <a-date-picker v-decorator="['endtime', {rules: [{required: true, message: '截至时间必须填写'}]}]"
            show-time
            valueFormat="YYYY-MM-DD HH:mm:ss"
            type="date"
            placeholder="截至时间"
            style="width: 100%;"
          />
        </a-form-item>
        <a-form-item label="任务描述">
          <a-input v-decorator="['descs', {rules: [{required: true, message: '请输入任务描述'}]}]" />
        </a-form-item>
        <a-form-item label="任务目标">
          <a-input v-decorator="['task_target', {rules: [{required: true, message: '请输入任务目标'}]}]" />
        </a-form-item>
        <a-form-item
            label="任务附件"
          >
          <a-upload
            action="index.php/?s=App.Examples_Upload.Go"
            :multiple="true"
            @change="handleCertChange"
            :fileList="certFileScource"
            :beforeUpload="beforeUpload"
          >
            <a-button> <a-icon type="upload" /> 上传附件 </a-button>
          </a-upload>
        </a-form-item>
      </a-form>
      
        
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 引入列表
import { getSchoolList, GetTaskList } from '../../../../api/school'
// 表单字段
const fields = ['id', 'task_name', 'taskcate_id', 'release_time', 'endtime', 'descs', 'task_target']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      // 任务附件
      certFileScource: [],
      taskname: '',
      // 表格数据
      jsonData: {},
      // 任务分类
			taskList: [],
      form: this.$form.createForm(this),
      headers: {
        authorization: 'authorization-text',
      },
    }
  },
  created () {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if(this.model && this.model.id > 0 && this.model.files != '') {
        const item = [{
          uid: this.model.id,
          name: this.model.files,
          status: 'done',
          url: this.model.files
        }]
        this.certFileScource = item
        console.log("this.certFileScource",this.certFileScource)
      } else {
        this.certFileScource = [];
      }
      
      this.taskname = this.model.task_name
      console.log('task_name', this.taskname)
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted() {
    // 调用方法获取分类列表
		this.UsersGetTaskList()
  },
  methods: { 
    beforeUpload(info) {
      console.log('---------------------------------,', info)
    },
    // 任务附件
		handleCertChange(info) {
      console.log(info);
      let { fileList } = info
      const status = info.file.status
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        this.$message.success(`${info.file.name} file uploaded successfully`);
        this.certFileScource = info.file.response.data.url
        this.$emit('getfiles', this.certFileScource)
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} file upload failed.`);
      }
      if( info.file.status === "removed" ){
        // this.$emit('getfiles', info.file.response.data.url)
        this.certFileScource = ''
        this.$emit('getfiles', this.certFileScource)
      }
        this.certFileScource = [...fileList] //重点
    },
    // 获取任务分类列表
		UsersGetTaskList () {
      GetTaskList().then(res => {
        this.taskList = res.data.data
      })
    },
    // 搜素模糊查询
      filterOption(input, option) {
        return (
      option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      }
  }
}
</script>
